<template>
	<view class="QrSite">
		<view>
			<view>选择收货地址</view>
		</view>
		<view class="location" @click="Golocation()">
			<span v-if="locationList == ''">添加收货地址</span>
			<span v-else v-for="(item,index) in locationList">
				<span>
					{{item.provinceCityDistrict}}
					{{item.detail}}
				</span>
			</span>
		</view>
		<view class="Goods_show">
			<view class="Goods_title">
				{{GoodsList.businessName}}
			</view>
			<view class="Goods_info">
				<view class="Goods_info_image">
					<image :src="GoodsList.image"></image>
				</view>
				<view class="Goods_info_public">
					<view class="Goods_info_title">
						<view class="">
							{{GoodsList.storeName}}
						</view>
						<view class="">
							￥{{GoodsList.price}}
						</view>
					</view>
					<view class="Goods_info_number">
						×{{GoodsList.cartNum}}
					</view>
					<view class="dikou" v-if="this.ShopPid!=15">
						<view class="yellow" @click="show = true" v-if="fuck">{{fuck}}</view>
						<view class="yellow" @click="show = true" v-else>点击选择支付方式</view>
					</view>
					<u-picker :show="show" ref="uPicker" :columns="columns" @confirm="confirm" @cancel="close">
					</u-picker>
				</view>
			</view>
			<view class="Goods_word">
				<view class="Goods_word_title">
					<view>买家留言</view>
					<textarea v-model="status" placeholder="可以填写与卖家达成的协议"></textarea>
				</view>
			</view>
		</view>
		<view class="Goods_Dxx">
			<view class="Goods_Dxx_price">
				<view>
					商品总金额
				</view>
				<view>
					￥{{Dprice}}
				</view>
			</view>
		</view>
		<view class="Boottom_button">
			<view class="B_title">
				合计金额:￥<span>{{Sprice}}.00 {{GoodsList.payType==5?'':GoodsList.payType==6?'消费券':'健康分'}}</span>
			</view>
			<view class="uni-btn-v">
				<button type="primary" @click="GetPay()">确认支付</button>
			</view>
		</view>
		<paypass ref="yspay" @cancel="handleClose" @submit="submitpay"></paypass>
	</view>
</template>

<script>
	import paypass from "@/components/ys-paypass/ys-paypass.vue";
	export default {
		components: {
			paypass
		},
		data() {
			return {
				GoodsList: [],
				Xprice: 0,
				Dprice: 0,
				Sprice: 0,
				postAge: 0,
				locationList: [],
				status: '',
				cateId: '',
				locationId: '',
				ShopPid: '',
				shopNum: '',
				payTypeList: [],
				statusA: '',
				checked: true,
				half: 0,
				adoptVoucherRMB: 0, //现金
				coupons: 0, //认养券
				cash: 0, //现有余额
				qwerqwer: 0,
				show: false,
				columns: [
					['认养券', '余额']
				],
				fuck: '',
			}
		},
		methods: {
			close() {
				this.show = false
			},
			confirm(e) {
				this.fuck = e.value[0]
				this.show = false
			},
			handleClose() {

			},

			handleSubmit(item) {
				if (this.half == 1) {
					item.amount = this.GoodsList.price / 2
				}
				let payType = [{
					payType: this.GoodsList.payType,
					orderNo: item.orderNo,
					price: item.amount
				}]
				this.$http({
					url: '/member/balanceJudgment',
					data: {
						listStr: JSON.stringify(payType),
					}
				}).then(res => {
					if (res.data.code === 200) {
						let bool = true
						res.data.data.forEach(k => {
							if (k.flag !== 0) {
								bool = false
							}
						})
						if (bool) {
							this.$refs.yspay.show();
						} else {
							uni.showToast({
								title: "认养券不足",
								icon: 'error'
							})
							setTimeout(() => {
								this.handleClose()
							}, 1000)
						}

					} else {
						uni.showToast({
							title: res.data.message,
							icon: 'error'
						})
						this.handleClose()
					}
				})
			},
			submitpay(ex) {
				if (this.ShopPid == 15) {
					this.half = 9
				}
				//不选择余额兑换时
				if (this.checked) {
					this.qwerqwer = 1
				} else {
					this.qwerqwer = 0
				}
				uni.showLoading({
					title: '请稍等',
					mask: true
				});
				let payType = this.payTypeList.map(item => {
					return item.orderNo
				})
				// #ifdef MP-WEIXIN
				setTimeout(() => {
					this.$http({
						url: '/member/balancePayment',
						data: {
							usageStatus: this.half,
							orderNo: JSON.stringify(payType),
							payPassword: ex.value,
							useAdoptionVoucher: this.qwerqwer,
							status: this.statusA
						}
					}).then(res => {
						if (res.data.code == 200) {
							uni.hideLoading();
							uni.showToast({
								title: '支付成功',
							})
							setTimeout(() => {
								uni.switchTab({
									url: '/pages/goods-category/goods-category'
								})
							}, 1000)
							this.handleClose()
						} else {
							uni.showToast({
								title: res.data.message,
								icon: 'error'
							}).then(this.Passshow = true)
						}
					})
				}, 3000)
				// #endif
				// #ifdef H5
				this.$http({
					url: '/member/balancePayment',
					data: {
						usageStatus: this.half,
						orderNo: JSON.stringify(payType),
						payPassword: ex.value,
						useAdoptionVoucher: this.qwerqwer,
						status: this.statusA
					}
				}).then(res => {
					if (res.data.code == 200) {
						uni.hideLoading();
						uni.showToast({
							title: '支付成功',
						})
						setTimeout(() => {
							uni.switchTab({
								url: '/pages/goods-category/goods-category'
							})
						}, 1000)
						this.handleClose()
					} else {
						uni.showToast({
							title: res.data.message,
							icon: 'error'
						}).then(this.Passshow = true)
					}
				})
				// #endif
				// #ifdef APP-PLUS
				this.$http({
					url: '/member/balancePayment',
					data: {
						usageStatus: this.half,
						orderNo: JSON.stringify(payType),
						payPassword: ex.value,
						useAdoptionVoucher: this.qwerqwer,
						status: this.statusA
					}
				}).then(res => {
					if (res.data.code == 200) {
						uni.hideLoading();
						uni.showToast({
							title: '支付成功',
						})
						setTimeout(() => {
							uni.switchTab({
								url: '/pages/goods-category/goods-category'
							})
						}, 1000)
						this.handleClose()
					} else {
						uni.showToast({
							title: res.data.message,
							icon: 'error'
						}).then(this.Passshow = true)
					}
				})
				// #endif
			},
			GetPay() {
				if (this.fuck) {
					if (this.fuck == '认养券+余额') {
						this.half = 1
						this.coupons = this.Dprice / 2
						this.adoptVoucherRMB = this.Dprice / 2
					} else if (this.fuck == '认养券') {
						this.half = 0
						this.adoptVoucherRMB = 0
					} else if (this.fuck == '余额') {
						this.half = 2
					}
				} else {
					uni.showToast({
						title: '请选择支付方式',
						icon: 'error'
					})
					return;
				}
				//只有买鸡的时候传值 adoptVoucherRMB
				if (this.ShopPid == 15) {
					this.adoptVoucherRMB = 0
				}
				// #ifdef H5
				let orderSrc = 1
				// #endif
				// #ifdef MP-WEIXIN
				let orderSrc = 2
				// #endif
				// #ifdef APP-PLUS
				let orderSrc = 3
				// #endif
				if (this.locationList.length === 0) {
					uni.showToast({
						title: '收货地址不能为空',
						icon: 'error'
					})
				} else {
					let arr = {
						usageStatus: this.half,
						orderSrc: '3', // 订单来源
						amount: this.Sprice, // 实际支付金额
						deliverMoney: this.postAge, // 运费
						harvestAddressId: this.locationList[0].id, // 收货地址ID
						totalAmount: this.Sprice, // 订单总金额
						num: this.GoodsList.cartNum,
						id: this.GoodsList.id, // 商品ID
						orderType: "0", // 订单类型
						deliveryStatus: "1", // 提货方式
						processingFee: 0,
						payType: this.GoodsList.payType,
						remark: this.status,
						crmAdoptionVouchers: this.coupons, //认养券抵扣
						crmCash: this.adoptVoucherRMB, //现金
					}
					this.$http({
						url: '/crmOrder/insCrmOrder',
						data: arr,
					}).then(res => {
						if (res.data.code === 200) {
							this.payTypeList = [res.data.data]
							this.handleSubmit(res.data.data)
						} else {
							uni.showToast({
								title: res.data.message,
								icon: 'error'
							})
						}
					})
				}
			},
			Golocation() {
				uni.navigateTo({
					url: '/pages/locationGo/locationGo'
				})
			},
			SearchLocation() {
				this.$http({
					url: '/crmUserAddress/selCrmUserAddress'
				}).then(res => {
					this.locationList = res.data.data
					res.data.data.map(item => {
						if (item.isDefault == 1) {
							this.locationId = item.id
						}
					})

				})
			},
			SearchGoods(id, num) {
				this.$http({
					url: '/crmOrder/confirmSettlement',
					data: {
						pid: id,
						num: num
					}
				}).then(res => {
					if (res.data.code === 200) {
						this.GoodsList = res.data.data
						this.adoptVoucherRMB = this.GoodsList.price
						this.Dprice = this.Dprice + this.GoodsList.price * this.GoodsList.cartNum
						this.postAge = this.postAge + this.GoodsList.postage
						this.Xprice = this.GoodsList.cartNum * this.GoodsList.price
						this.Sprice = this.postAge + this.Dprice
						this.SearchLocation()
					} else {
						uni.showToast({
							title: res.data.message,
							icon: 'error'
						})
						setTimeout(() => {
							uni.switchTab({
								url: '/pages/goods-category/goods-category'
							})
						}, 1000)
					}

				})
			}
		},
		onLoad(option) {
			this.SearchGoods(option.pid, option.num)
			this.ShopPid = option.pid
			this.shopNum = option.num
			this.cateId = option.pid
			if (option.id != 15) {
				this.statusA = 4
			}
		},
		onShow() {
			// let id = localStorage.getItem("userSelect")
			let id = uni.getStorageSync('userSelect')
			if (id) {
				this.$http({
					url: '/crmUserAddress/selUpdAddress',
					method: "GET",
					data: {
						id
					}
				}).then(res => {
					if (res.data.code === 200) {
						this.locationList = [res.data.data]
						localStorage.removeItem("userSelect")
					} else {
						uni.showToast({
							title: '请先填写收货地址',
							icon: 'error'
						})
					}
				})
			} else {
				this.SearchLocation()
			}
			this.$http({
				url: "/member/getMyPage"
			}).then(res => {
				this.cash = res.data.data.cash
				this.cash1 = res.data.data.cash1
			})

		}
	}
</script>

<style>
	@import './index.css';
</style>
